//
//  QYGCoinController.m
//  wawaji_ios
//
//  Created by 奇艺果 on 2017/12/29.
//  Copyright © 2017年 yiguo qi. All rights reserved.
//

#import "QYGCoinController.h"
#import "QYGNetRequestModel.h"
#import "QYGRechargeView.h"
#import "DDCoverView.h"
#import "QYGCoinRecordModel.h"
#import "QYGAccountCell.h"

static NSString *coinIdentifier = @"coinIdentifier";
@interface QYGCoinController ()<UITableViewDelegate, UITableViewDataSource>


@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger page;

@end

@implementation QYGCoinController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"娃娃币";
    self.view.backgroundColor = [UIColor cm_backgroudColor];
    [self setupUI];
    [self setupData];
    WS(weakSelf);
    [[SGUtil shareUtil] setRechargeBlock:^(BOOL isSuccess, QYGCoinListModel *coinListModel) {
        [weakSelf.view makeToast:isSuccess ? @"支付成功！" :@"支付失败！"];
        if(isSuccess){
            weakSelf.userInfo.coin =  weakSelf.userInfo.coin + coinListModel.coin + coinListModel.coin_award ;
            weakSelf.userInfo.coin_buy = weakSelf.userInfo.coin_buy + coinListModel.coin + coinListModel.coin_award;
            [weakSelf.tableView.mj_header beginRefreshing];
        }
    }];
}

- (void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    self.tableView.x = 0;
    self.tableView.y = 0;
    self.tableView.width = SCREEN_WIDTH;
    self.tableView.height = SCREEN_HEIGHT;
    
}
#pragma mark setUI

- (void)setupUI{
    
    [self.view addSubview:self.tableView];
    WS(weakSelf);
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [weakSelf loadNewData];
    }];
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [weakSelf loadMoreData];
    }];
}

#pragma  mark - tableviewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return ScaleH(61);
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return ScaleH(105);
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1f;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    QYGAccountHeader *accountHeaderVC = [[QYGAccountHeader alloc] init];
    [accountHeaderVC accountHeaderWithNameStr:[NSString stringWithFormat:@"%ld",self.userInfo.coin] detailStr:@"金币余额"];
    WS(weakSelf);
    [accountHeaderVC setAccountHeaderRechargeBlock:^{
        [weakSelf getRechargeList];
    }];
    return accountHeaderVC;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    QYGAccountCell *cell = [tableView dequeueReusableCellWithIdentifier:coinIdentifier forIndexPath:indexPath];
    
    QYGCoinRecordModel *coinRecordModel = self.dataArray[indexPath.row];
    NSString *coinType = nil;
    if([coinRecordModel.recordable_type isEqualToString:@"payment"]){
        coinType = @"充值";
    }else if([coinRecordModel.recordable_type isEqualToString:@"game_record"]){
        
        coinType = @"游戏消费";
    }else if ([coinRecordModel.recordable_type isEqualToString:@"payment_award"]){
        
        coinType = @"充值赠送";
    }else if ([coinRecordModel.recordable_type isEqualToString:@"register"]){
        
        coinType = @"新用户注册";
    }else if ([coinRecordModel.recordable_type isEqualToString:@"system"]){
        
        coinType = @"系统赠送";
    }
    NSString *colorStr = coinRecordModel.coin > 0 ? @"09bb07" : @"ffb52b";
    NSString *accessoryStr = coinRecordModel.coin > 0 ? [NSString stringWithFormat:@"+%ld",coinRecordModel.coin] : [NSString stringWithFormat:@"%ld",coinRecordModel.coin];
    [cell accountCellWithNameStr:coinType detailStr:coinRecordModel.created_at accessoryStr:accessoryStr accessoryColor:[UIColor colorWithHex:colorStr]];
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
- (void)dealloc{
    [NetRequestClass cancelOperations];
}
- (void)setupData{
    [self.tableView.mj_header beginRefreshing];
}
#pragma mark - loadData
- (void)loadDataWithPage:(NSInteger)page{
    
    NSInteger per_page = 20;
    [QYGNetRequestModel getCoinRecordsWithPage:page per_page:per_page Completed:^(NSArray *coinRecordArr) {
        [self endRefresh];
        [self.dataArray addObjectsFromArray:coinRecordArr];
        [self.tableView reloadData];
        self.page = page;
    } WithErrorCodeBlock:^(NSError *error) {
        [self endRefresh];
    }];
}
- (void)endRefresh{
    [self.tableView.mj_header endRefreshing];
    [self.tableView.mj_footer endRefreshing];
}

- (void)loadMoreData{
    [self loadDataWithPage:self.page + 1];
}
- (void)loadNewData {
    
    self.dataArray = [NSMutableArray array];
    
    [self loadDataWithPage:1];
    [self getUserInfoData];
}

// 获取用户信息
- (void)getUserInfoData{
    
    [QYGNetRequestModel getUserInfoCompleted:^(QYGUserInfoModel *userInfo) {
        self.userInfo.coin = userInfo.coin;
        self.userInfo.coin_buy = userInfo.coin_buy;
        [self.tableView reloadData];
    } WithErrorCodeBlock:^(NSError *error) {
    }];
}
#pragma mark target
- (IBAction)rechargeBtnClick:(id)sender {
    [self getRechargeList];
}

- (void)getRechargeList{
    NSLog(@"充值");
    WS(weakSelf);
    [SVProgressHUD show];
    [QYGNetRequestModel getCoinsListCompleted:^(NSArray *coinListArr) {
        [SVProgressHUD dismiss];
        [weakSelf rechargeWithCoinListArr:coinListArr];
    } WithErrorCodeBlock:^(NSError *error) {
    }];
}
- (void)rechargeWithCoinListArr:(NSArray *)coinListArr{
    
    if(!coinListArr.count){
        [self.view makeToast:@"没有数据"];
        return;
    }
    CGFloat h = ScaleW(70);
    if(coinListArr.count > 4){
        h = h + ScaleW(82.5) * 4.5;
    }else{
        h = h + ScaleW(82.5) * coinListArr.count;
    }
    QYGRechargeView *rechargeView = [[QYGRechargeView alloc] initWithFrame:CGRectMake(0, 0, ScaleW(315),h)];
    rechargeView.coinListArr = coinListArr;
   
    DDCoverView *coverView = [[DDCoverView alloc] initWithSize:rechargeView.size subView:rechargeView];
    coverView.isShowCancelImage = YES;
    [coverView showInView:KeyWindow];
    
    __weak typeof(coverView) weakCoverView = coverView;
    
    [rechargeView setCloseBlock:^{
        [weakCoverView hidden];
    }];
    WS(weakSelf);
    [rechargeView setRechargeBlock:^(BOOL isSuccess, QYGCoinListModel *coinListModel) {
        [weakSelf.view makeToast:isSuccess ? @"支付成功！" :@"支付失败！"];
        if(isSuccess){
            weakSelf.userInfo.coin =  weakSelf.userInfo.coin + coinListModel.coin + coinListModel.coin_award ;
            weakSelf.userInfo.coin_buy = weakSelf.userInfo.coin_buy + coinListModel.coin + coinListModel.coin_award;
            [weakSelf.tableView.mj_header beginRefreshing];
        }
        [weakCoverView hidden];
    }];
}

#pragma mark - lazyLoad
- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [UIView new];
        _tableView.estimatedRowHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        CGFloat navH = self.navigationController.navigationBar.height;
        _tableView.contentInset = UIEdgeInsetsMake(0, 0, navH + 20, 0);
        _tableView.separatorStyle = UITableViewCellSelectionStyleNone;
        
        [_tableView registerClass:[QYGAccountCell class] forCellReuseIdentifier:coinIdentifier];
        
    }
    return _tableView;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
